﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Workflow
{
	/// <summary>
	/// Summary description for ExcelExport.
	/// </summary>
	public class ExcelExport : System.Web.UI.Page
	{
		private int id;
		private string type;
		private string transition;

		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				//<--Joan 2007-08-08 meeting minutes export
				if (Request.QueryString["MMID"]!=null && Request.QueryString["MMID"] != "")
				{
					this.id=Convert.ToInt32(Request.QueryString["MMID"]);
					this.type="MM";
					if (this.id<=0)
					{
						Page.RegisterClientScriptBlock("","<script>alert('Please save meeting minutes first.\\nIf you already saved, please contact the system admin.');</script>");
						return;
					}
				}
				//-->end
				else
				{
					if (Request.QueryString["AppID"]!=null && Request.QueryString["AppID"] != "")
						this.id=Convert.ToInt32(Request.QueryString["AppID"]);
					else
						this.id=0;			
					if (this.id<=0)
					{
						BEGDC.CC.Web.UserControls.IPageAccess page = (UserControls.IPageAccess)this.Page;
						this.id=page.Access.AppId;
					}
					this.type =Request.QueryString["PDType"];
			
					if (this.id <= 0)
						this.Visible=false;
						//this.btnExport.Visible=false;
					else
						this.Visible=true;;
					//this.btnExport.Visible=true;

					if (this.type.ToUpper()=="CR")
						this.transition=Request.QueryString["Transition"];			


					if (this.id<=0)
					{
						Page.RegisterClientScriptBlock("","<script>alert('Please save the application form first.\\nIf you already saved, please contact the system admin.');</script>");
						return;
					}
				}

				ExcelTool.ICCExcelExport excelExport=null;
				BEGDC.CC.BL.Common.Query.IQuery queryService =  AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;

				Application app=queryService.GetApplication(this.id);
				switch (type)
				{
					case "EV":
						excelExport=new ExcelTool.ExportEVForm();
						break;
					case "CV":
						excelExport=new ExcelTool.ExportCVForm();
						break;
					case "CO":
						excelExport=new ExcelTool.ExportCOForm();
						break;
					case "CR":
//						Application app=queryService.GetApplication(this.id);
						if (app.IsTransition)
						{	
							//CR-A
							excelExport=new ExcelTool.ExportCRFormA();
						}
						else
						{
							//CR-B
							excelExport=new ExcelTool.ExportCRFormB();
						}						
						break;
					case "OCV":
						excelExport=new ExcelTool.ExportOCVForm();
						break;
					case "OCR":						
						if (app.IsTransition)
						{	
							//OCR-A
							excelExport=new ExcelTool.ExportOCRFormA();
						}
						else
						{
							//OCR-B
							excelExport=new ExcelTool.ExportOCRFormB();
						}	
						break;
					case "EBDCV":
						excelExport=new ExcelTool.ExportEBDCVForm();
						break;
					case "EBD":						
						if (app.IsTransition)
						{	
							//EBD-A
							excelExport=new ExcelTool.ExportEBDFormA();
						}
						else
						{
							//EBD-B
							excelExport=new ExcelTool.ExportEBDFormB();
						}	
						break;
					case "ESCCV":
						excelExport=new ExcelTool.ExportESCCVForm();
						break;
					case "ESC":						
						if (app.IsTransition)
						{	
							//ESC-A
							excelExport=new ExcelTool.ExportESCFormA();
						}
						else
						{
							//ESC-B
							excelExport=new ExcelTool.ExportESCFormB();
						}	
						break;
					case "MM":
						excelExport=new ExcelTool.ExportEVMeeting();
						break;
					default:
						break;
			
				}
				excelExport.Export(this,this.id);
			}
			catch(Exception ex)
			{
                //Response.Write("<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');window.close();</script>");
                throw;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
